﻿#region Disclaimer / License
// Copyright (C) 2011, Kenneth Bergeron, IAP Worldwide Services, Inc
// NOAA :: National Marine Fisheries Service
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;

namespace CatchProcessing
{
    using LocalDataSet = DataSets.LocalDataSet;
    using EASASettingsDataSet = DataSets.EASASettingsDataSet;
    /// <summary>
    /// Add/Edit a Sample record
    /// </summary>
    public partial class SampleDialog : Form
    {
        /// <summary>
        /// The Fish row form which this child came
        /// </summary>
        private LocalDataSet.FishRow ParentRow;
        /// <summary>
        /// Row being edited
        /// </summary>
        private LocalDataSet.FishSamplesRow Row;
        /// <summary>
        /// Add/Edit a Sample record
        /// </summary>
        public SampleDialog(LocalDataSet.FishRow aParentRow)
        {
            InitializeComponent();
            Bind();
            this.ParentRow = aParentRow;
            tabControl1_SelectedIndexChanged(this, null);
        }
        /// <summary>
        /// Set up the varios control's bining
        /// </summary>
        private void Bind()
        {
            this.measurementGeneralCodesBindingSource.SuspendBinding();
            this.lengthSampleCodesBindingSource.SuspendBinding();
            this.tagCodesBindingSource.SuspendBinding();
            this.EASASettingsDataSet = DataSets.Glob.Support;
            this.measurementGeneralCodesBindingSource.DataSource = this.EASASettingsDataSet.MeasurementGeneralCodes;
            this.lengthSampleCodesBindingSource.DataSource = this.EASASettingsDataSet.LengthSampleCodes;
            this.tagCodesBindingSource.DataSource = this.EASASettingsDataSet.TagCodes;
            this.tagCodesBindingSource.ResumeBinding();
            this.measurementGeneralCodesBindingSource.ResumeBinding();
            this.lengthSampleCodesBindingSource.ResumeBinding();
            this.tagCodesBindingSource.ResumeBinding();
        }
        /// <summary>
        /// Looks in a ComboBox's items for a value; if found, sets SelectedItem
        /// </summary>
        /// <param name="aCombo">Look in this</param>
        /// <param name="aValue">For this</param>
        private void FindValue(ComboBox aCombo, string aValue)
        {
            object Result = (from object Item in aCombo.Items 
                    where ComboRow(Item)["CODE"].Equals(aValue) 
                    select Item).DefaultIfEmpty(null).First();
            if (Result != null)
                aCombo.SelectedItem = Result;
        }
        /// <summary>
        /// Makes the 'NEW' style.
        /// </summary>
        /// <param name="aNew">On (true) or Off</param>
        private void SetNew(bool aNew)
        {
            this.toolStrip1.BackColor = aNew ? Color.LawnGreen : this.BackColor;
            this.NewToolStripLabel.Text = aNew ? "N E W" : "E D I T";
        }
        /// <summary>
        /// Selected sample type
        /// </summary>
        private LocalDataSet.FishSamplesRow.SampleType Sample = LocalDataSet.FishSamplesRow.SampleType.Sample;
        /// <summary>
        /// User changed sample types
        /// </summary>
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (TabPage T in this.tabControl1.TabPages)
                T.ImageIndex = (T == this.tabControl1.SelectedTab) ? 1 : 0;
            Sample = (LocalDataSet.FishSamplesRow.SampleType)this.tabControl1.SelectedIndex;
        }
        /// <summary>
        /// Set controls from row
        /// </summary>
        /// <param name="aRow">Row to set</param>
        public void SetRow(LocalDataSet.FishSamplesRow aRow)
        {
            this.Row = aRow;
            // Set the header
            this.Text =  "Sample / Measurement / Tag  " + this.ParentRow.VesselName;
            this.txtCruise.Text = this.ParentRow.Cruise;
            this.StationToolStripLabel.Text = this.ParentRow.Station;
            this.IdTextBox.Text = aRow.HookID.ToString();

            if (aRow.IsRetrievedNull()) aRow.Retrieved = true;
            Sample = aRow.Sample;
            this.tabControl1.SelectedIndex = (int)Sample;
            switch (Sample)
            {
                case LocalDataSet.FishSamplesRow.SampleType.Sample:
                    {
                        FindValue( this.SampleComboBox, Row.CODE);
                        this.SampleNumericUpDown.Value = (decimal)aRow.Value;
                        this.PurTextBox.Text = aRow.Identifier;
                        this.ReqTextBox.Text = aRow.Description;
                        this.SampleCommentTextBox.Text = aRow.Comment;
                    }
                    break;
                case LocalDataSet.FishSamplesRow.SampleType.Measurement:
                    {
                        FindValue( this.LengthComboBox, Row.CODE);
                        this.LengthNumericUpDown.Value = (decimal)aRow.Value;
                    }
                    break;
                case LocalDataSet.FishSamplesRow.SampleType.Tag:
                    {
                        FindValue( this.TagComboBox, Row.CODE);
                        this.TagCommentTextBox.Text = aRow.Comment;
                        this.TagNumberTextBox.Text = aRow.Identifier;
                        this.RetrievedRadioButton.Checked = aRow.Retrieved;
                        this.DeployRadioButton.Checked = !aRow.Retrieved; // ??
                    }
                    break;
            }
        }
        /// <summary>
        /// Returns a comboBox item as a datarow
        /// </summary>
        /// <param name="aItem">ComboBox item</param>
        /// <returns>DataRow</returns>
        private System.Data.DataRow ComboRow(object aItem)
        {
            return (System.Data.DataRow)((System.Data.DataRowView)aItem).Row;
        }
        /// <summary>
        /// Saves the controls to the Row
        /// </summary>
        /// <param name="aRow">Row to save control values</param>
        public void GetRow(LocalDataSet.FishSamplesRow aRow)
        {
            aRow.Sample = Sample;
            switch (Sample)
            {
                case LocalDataSet.FishSamplesRow.SampleType.Sample:
                    {
                        aRow.CODE = (string)ComboRow(this.SampleComboBox.SelectedItem)["CODE"];
                        aRow.Value = (double)this.SampleNumericUpDown.Value;
                        aRow.Identifier = this.PurTextBox.Text;
                        aRow.Description = this.ReqTextBox.Text;
                        aRow.Comment = this.SampleCommentTextBox.Text;
                    }
                    break;
                case LocalDataSet.FishSamplesRow.SampleType.Measurement:
                    {
                        aRow.CODE = (string)ComboRow(this.LengthComboBox.SelectedItem)["CODE"];
                        aRow.Value = (double)this.LengthNumericUpDown.Value;
                    }
                    break;
                case LocalDataSet.FishSamplesRow.SampleType.Tag:
                    {
                        aRow.CODE = (string)ComboRow(this.TagComboBox.SelectedItem)["CODE"];
                        aRow.Identifier = this.TagNumberTextBox.Text;
                        aRow.Comment = this.TagCommentTextBox.Text;
                        aRow.Retrieved = this.RetrievedRadioButton.Checked;
                    }
                    break;
            }
        }
        /// <summary>
        /// Used to only look at an entry
        /// </summary>
        /// <param name="aRow">Row to look at</param>
        /// <returns>OK</returns>
        public DialogResult ViewDialog(LocalDataSet.FishSamplesRow aRow)
        {
            this.OKButton.Visible = false;
            this.CanButton.Text = "OK";
            this.tabControl1.Enabled = false;
            return ShowDialog(aRow, true);
        }
        /// <summary>
        /// Add/Edit a Samples row
        /// </summary>
        /// <param name="aRow">Row to edit</param>
        /// <param name="aIsEdit">True to edit, else add</param>
        /// <returns>DialogResult</returns>
        public DialogResult ShowDialog(LocalDataSet.FishSamplesRow aRow, bool aIsEdit)
        {
            SetRow(aRow);
            SetNew(!aIsEdit);
            if (aIsEdit)
            {
                if (Sample != LocalDataSet.FishSamplesRow.SampleType.Measurement) this.tabControl1.TabPages.Remove(this.LengthTabPage);
                if (Sample != LocalDataSet.FishSamplesRow.SampleType.Sample) this.tabControl1.TabPages.Remove(this.SampleTabPage);
                if (Sample != LocalDataSet.FishSamplesRow.SampleType.Tag) this.tabControl1.TabPages.Remove(this.TagTabPage);
                this.tabControl1.TabPages[0].ImageIndex = 1;
            }
            return ShowDialog();
        }
        /// <summary>
        /// Pressed OK, save and close
        /// </summary>
        private void OKButton_Click(object sender, EventArgs e)
        {
            GetRow(this.Row);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        /// <summary>
        /// Pressed Cancel, close
        /// </summary>
        private void CanButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }
    }
}
